<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<script>
    // 创建验证码字符
    function createCharacter() {
        // 数字
        let number = [];
        // 大写英文
        let letter = [];
        for (let i = 0; i < 26; i++) {
            letter[i] = String.fromCharCode(65 + i)
            if (i < 10) {
                number[i] = i
            }
        }
        // 连接数字英文
        return number.concat(letter)
    }

    // 随机数的下标 0-35
    function myRandom(min, max) {
        return Math.floor(Math.random() * (max - min) + min)
    }

    // 随机 颜色
    function randomColor() {
        let r = myRandom(0, 256);
        let g = myRandom(0, 256);
        let b = myRandom(0, 256);
        // 用模板字符串(ES6) 代替原来的 + 拼接
        return `rgb(${r},${g},${b})`;
    }

    // 四位随机的字符
    let template = createCharacter()
    console.log(template)
    let str = ""
    for (let i = 0; i < 4; i++) {
        str += template[myRandom(0, 36)]
    }

    document.body.style.backgroundColor = randomColor()
    document.write(`<h1>${str}<h1>`)
</script>
</body>

</html>

</html>
